local skel = fk.CreateSkill {
  name = "rmt__rangyi",
}

skel:addEffect(fk.CardUsing, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and (data.card.name == "jink" or data.card.name == "nullification") then
      return table.find(player.room.alive_players, function (p)
        return p ~= target and p:getHandcardNum() > 1 and not table.contains(player:getTableMark(skel.name), p.id)
      end)
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p)
      return p ~= target and p:getHandcardNum() > 1 and not table.contains(player:getTableMark(skel.name), p.id)
    end)
    local tos = room:askToChoosePlayers(player, {
      targets = targets,
      max_num = 1, min_num = 1, skill_name = skel.name, cancelable = true,
      prompt = "#rmt__rangyi-choose", target_tip_name = "rmt__rangyi_tip",
    })
    if #tos > 0 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    room:addTableMark(player, skel.name, to.id)
    local num = to:getHandcardNum() // 2
    local cards = room:askToChooseCards(player, {
      target = to, max = num, min = num, flag = "h", skill_name = skel.name,
    })
    room:throwCard(cards, skel.name, to, player)
    if not player.dead and table.every(room.alive_players, function (p)
      return table.contains(player:getTableMark(skel.name), p.id)
    end) then
      room:setPlayerMark(player, skel.name, 0)
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, skel.name, 0)
end)

-- 每个人死亡后检测一下是否满足重置条件
skel:addEffect(fk.Deathed, {
  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(skel.name, true) then
      return table.every(player.room.alive_players, function (p)
        return table.contains(player:getTableMark(skel.name), p.id)
      end)
    end
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, skel.name, 0)
  end,
})

Fk:addTargetTip({
  name = "rmt__rangyi_tip",
  target_tip = function (self, player, to_select, selected, selected_cards, card, selectable, extra_data)
    if table.contains(player:getTableMark(skel.name), to_select.id) then
      return "@rmt__rangyi_record"
    elseif to_select:getHandcardNum() < 2 then
      return { {content = "@rmt__rangyi_cardnum", type = "warning"} }
    end
  end,
})

Fk:loadTranslationTable{
  ["rmt__rangyi"] = "攘夷",
  [":rmt__rangyi"] = "一名角色使用【闪】或【无懈可击】时，你可以弃置未以此法弃置过的另一名角色半数手牌（向下取整）；若你以此法弃置过所有存活角色的牌，重置此技能。",
  ["#rmt__rangyi-choose"] = "攘夷：你可以弃置一名角色一半的手牌！",
  ["rmt__rangyi_tip"] = "攘夷",
  ["@rmt__rangyi_cardnum"] = "牌不够",
  ["@rmt__rangyi_record"] = "已弃过",

  ["$rmt__rangyi1"] = "受先君专征之任，内修政治，外攘夷狄。",
  ["$rmt__rangyi2"] = "尔贡包茅不入，王祭无以缩酒，寡人是征。",
}

return skel
